import { querySelect, queryFakeList, deleteTableData, export_list, export_model } from '../services/api';

export default {
  namespace: 'list',

  state: {
    list: [],
    modelList: [],
    selectDrop: [],
    // 一级分类 保存数据
    oneTemplate: {
      value: '',
      text: '',
      aliasName: '',
      skuCode: null,
      fullName: null
    },
    oneStore: {
      value: '',
      text: '',
      aliasName: '',
      skuCode: null,
      fullName: null
    },
    // 下拉对应的值
    oneSkuCode: '',
    oneAliasName: '',
    // 二级分类
    twoTemplate: {
      value: '',
      text: '',
      aliasName: '',
      skuCode: null,
      fullName: null
    },
    twoSkuCode: '',
    twoAliasName: '',
    twoStore: {
      value: '',
      text: '',
      aliasName: '',
      skuCode: null,
      fullName: null
    },
    // 三级分类

    // sku列表
    // sku列表详情页面
    skuDetailData: [],
    //查询服务率
    serviceRate: []
  },

  effects: {
    *fetch({ payload, url }, { call, put }) {
      const response = yield call(queryFakeList, payload, url);
      yield put({
        type: 'queryList',
        payload: response,
      });
    },
    //删除表格数据
    *delete({ payload, url }, { call, put }) {
      const response = yield call(deleteTableData, payload, url);
      yield put({
        type: 'deleteData',
        payload: response,
      });
    },
    //导出数据
    *exportOrder({ payload, url }, { call, put }) {
      const response = yield call(export_list, payload, url);
      yield put({
        type: 'exportOrder',
        payload: response,
      });
    },
    *appendFetch({ payload }, { call, put }) {
      const response = yield call(queryFakeList, payload);
      yield put({
        type: 'appendList',
        payload: response,
      });
    },
    //查询模态框的数据
    *queryModel({ payload, url }, { call, put }) {
      const response = yield call(export_model, payload, url);
      yield put({
        type: 'queryModelData',
        payload: response,
      });
    },

    //查询下拉搜索
    *queryDrop({ payload, index }, { call, put }) {
      const response = yield call(querySelect, payload);
      yield put({
        type: 'queryDropSelect',
        payload: response,
      });
    },

    // 查询详情页面的数据
    *queryAddSkuDetail({ payload, url }, { call, put }) {
      const response = yield call(queryFakeList, payload, url);
      console.log(response);
        yield put({
          type: 'queryAddSkuList',
          payload: response,
        });
    },

    // 查询服务率
    *queryRate({ payload, url }, { call, put }) {
      const response = yield call(queryFakeList, payload, url);
      console.log(response);
        yield put({
          type: 'queryRateRd',
          payload: response,
        });
    },

  },

  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    // addsku详情页面的数据
    queryAddSkuList(state, action) {
      return {
        ...state,
        skuDetailData: action.payload,
      };
    },
    // 查询rate
    queryRateRd(state, action) {
      return {
        ...state,
        serviceRate: action.payload,
      };
    },
    //删除
    deleteData(state, action) {
      return {
        ...state,
        deleteResult: action.payload 
      }
    },
    //导出文件
    exportOrder(state, action) {
      return {
        ...state,
        result: action.payload 
      }
    },
    //加入数据
    appendList(state, action) {
      return {
        ...state,
        list: state.list.concat(action.payload),
      };
    },
    //查询模态框的数据
    queryModelData(state, action) {
      return {
        ...state,
        modelList: action.payload,
      };
    },

    //下拉搜索查询
    queryDropSelect(state, action) {
      console.log('下拉搜索');
      console.log(action);
      return {
        ...state,
        selectDrop: action.payload,
      };
    },

    // 给一级分类赋值
    saveTemplate(state, action) {
      console.log(action);
      switch(action.payload.index) {
        case 0: 
          return {
            ...state,
            oneTemplate: {
              value: action.payload.value,
              text: action.payload.text,
              aliasName: action.payload.aliasName,
              skuCode: action.payload.skuCode,
              fullName: action.payload.fullName
            }
          }
          break;
        case 1:
        return {
          ...state,
          twoTemplate: {
            value: action.payload.value,
            text: action.payload.text,
            aliasName: action.payload.aliasName,
            skuCode: action.payload.skuCode,
            fullName: action.payload.fullName
          }
        }
          break;
        case 2: 
          break;
        default:
          return {
            ...state
          }      
      }
    },

    // 下拉赋值
    makeDropValReducer(state, action) {
      switch(action.data.index) {
        case 0:
          return {
            ...state,
            oneSkuCode: action.data.skuCode,
            oneAliasName: action.data.aliasName,
            oneTemplate: {
              value: action.payload.value,
              text: action.payload.text,
              aliasName: action.payload.aliasName,
              skuCode: action.data.skuCode,
              fullName: action.payload.fullName
            }
          };
          break;
        case 1: 
        return {
          ...state,
          twoSkuCode: action.data.skuCode,
          twoAliasName: action.data.aliasName,
          twoTemplate: {
            value: action.payload.value,
            text: action.payload.text,
            aliasName: action.payload.aliasName,
            skuCode: action.data.skuCode,
            fullName: action.payload.fullName
          }
        };
          break;
        default:
          return {
            ...state
          }    
      }
    },

    // 保存
    makeVals(state, action) {
      console.log(action);
      switch(action.data.index) {
        case 0:
          return {
            ...state,
            oneStore: {
              value: action.data.value,
              text: action.data.text,
              aliasName: action.data.aliasName,
              skuCode: action.data.skuCode,
              fullName: action.data.fullName
            }
          }
          break;
        case 1:
        return {
          ...state,
          twoStore: {
            value: action.data.value,
            text: action.data.text,
            aliasName: action.data.aliasName,
            skuCode: action.data.skuCode,
            fullName: action.data.fullName
          }
        }
          break;
        default:
          return {
            ...state
          }   
      }
    }
  },
};
